package com.lck.main.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        c1();
    }
    public static void c1(){
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        try {
            System.out.println("客户端已经成功启动");
            socket = new Socket(InetAddress.getLocalHost(), 8080);
            os = socket.getOutputStream();
//            os.write("你好服务端".getBytes());
            Scanner sc = new Scanner(System.in);
            String content = "客户端:1";
            os.write(content.getBytes());
//            sc.next(); 获取控制台内容，以空格分隔。只要是next方法都会造成阻塞。
            while (true){
//                String content = sc.nextLine();//获取控制台整行内容。
                is = socket.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                len = is.read(buffer);
                String s = new String(buffer, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
                e.printStackTrace();
        } finally{
            try {
                is.close();
                os.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
